package com.wash.shoes.entity;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.ruoyi.common.annotation.Excel;
import com.wash.shoes.common.enums.FactoryServiceTypeEnum;
import com.wash.shoes.common.enums.FactoryStatusEnum;
import com.wash.shoes.common.enums.FactoryTypeEnum;
import com.wash.shoes.common.serializer.ServiceTypeJsonSerializer;
import com.wash.shoes.common.typehandler.FactoryServiceTypeHandler;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@Data
@TableName(value = "os_factory", autoResultMap = true)
public class FactoryPo extends BasePo {
    @TableId(type = IdType.ASSIGN_ID)
    @Excel(name = "id")
    private String id;
    /**
     * 工厂图标
     * 
     * @TableField("factory_icon") private String factoryIcon;
     */
    /**
     * 工厂负责人
     */
    @TableField("factory_manager")
    @Excel(name = "工厂负责人")
    private String factoryManager;
    /**
     * 工厂名称
     */
    @TableField("factory_name")
    @Excel(name = "工厂名称")
    private String factoryName;
    /**
     * 工厂编号
     * 
     * @TableField("factory_code") private String factoryCode;
     */
    /**
     * 工厂电话
     */
    @TableField("factory_phone")
    @Excel(name = "工厂电话")
    private String factoryPhone;
    /**
     * 工厂地址
     */
    @TableField("factory_address")
    @Excel(name = "工厂地址")
    private String factoryAddress;

    /**
     * 省份
     */
    private String provinces;
    /**
     * 市
     */
    private String city;
    /**
     * 县/区
     */
    private String country;

    /**
     * 工厂状态
     */

    private FactoryStatusEnum status;

    @Excel(name = "工厂状态")
    @TableField(exist = false)
    private String statusName;

    /**
     * 工厂类型
     */
    @TableField(value = "factory_type")
    private FactoryTypeEnum factoryType;

    @Excel(name = "工厂类型")
    @TableField(exist = false)
    private String factoryTypeName;

    /**
     * 服务类型
     */
    @TableField(value = "service_type", typeHandler = FactoryServiceTypeHandler.class)
    @JsonSerialize(using = ServiceTypeJsonSerializer.class)
    private List<FactoryServiceTypeEnum> serviceType;

    @Excel(name = "服务类型")
    @TableField(exist = false)
    private String serviceTypeName;

    /**
     * 洗护正常时效
     */
    @TableField("normal_washing_duration")
    @Excel(name = "洗护正常时效")
    private Integer normalWashingDuration;// Normal washing and care duration

    /**
     * 洗护逾期时效
     */
    @TableField("expiration_date_for_cleaning")
    @Excel(name = "洗护逾期时效")
    private Integer ExpirationDateForCleaning;
    /**
     * 日均产能
     */
    @TableField("daily_capacity")
    @Excel(name = "日均产能")
    private Integer capacityOfProd;

    /**
     * 仓库释放规则
     */
    @TableField("warehouse_release_rule")
    private Integer WarehouseReleaseRule;
    /**
     * TODO 需增加覆盖品牌和覆盖区域返回字段
     */

    /**
     * 覆盖区域
     */
    @TableField(exist = false)
    private String coveringArea;
    /**
     * 覆盖品牌
     */
    @TableField(exist = false)
    private String coveringBrand;

    /**
     * 覆盖区域的code，用于前端回显工厂范围，省市
     */
    @TableField(exist = false)
    private String code;

    public void setStatus(FactoryStatusEnum status) {
        this.status = status;
        if (this.status != null) {
            this.statusName = this.status.getName();
        }
    }

    public void setFactoryType(FactoryTypeEnum factoryType) {
        this.factoryType = factoryType;
        if (this.getFactoryType() != null) {
            this.factoryTypeName = this.getFactoryType().getName();
        }
    }

    public void setServiceType(List<FactoryServiceTypeEnum> serviceType) {
        this.serviceType = serviceType;
        if (!CollectionUtils.isEmpty(this.getServiceType())) {
            List<String> serviceTypeNameList =
                this.getServiceType().stream().map(FactoryServiceTypeEnum::getName).collect(Collectors.toList());
            this.serviceTypeName = String.join(",", serviceTypeNameList);
        }
    }
}
